package com.unibuy.smartdevice.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.database.ProductsDao;
import com.unibuy.smartdevice.databinding.ActivityProductListBinding;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainSchedulerOnClick;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.ui.recycler.RecyclerProductListAdpter;
import com.unibuy.smartdevice.ui.tools.ImageGlide;
import com.unibuy.smartdevice.utils.LanguageHelper;

import java.util.HashMap;
import java.util.Map;

public class ProductListActivity extends AppCompatActivityAbstract {
    public enum Option {
        TOAST(0),
        RESET_DATA(1),
        ;

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }

    public static final Map<Integer, Option> optionMap = new HashMap<>();
    static {
        for (Option option : Option.values()) {
            optionMap.put(option.getOption(), option);
        }
    }

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return this.getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                Option option = optionMap.get(commandCode);
                switch (option) {
                    case TOAST:
                        super.execute(context, commandCode, message);
                        break;
                    case RESET_DATA:
                        int size = MyApp.getInstance().getProductList().size();
                        for (int index=0; index < size; index++) {
                            recyclerProductListAdpter.notifyItemChanged(index);
                        }
                        break;
                }
            }
        };
    }

    private ActivityProductListBinding binding;
    private RecyclerProductListAdpter recyclerProductListAdpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 讀取最後設定語言的設定
        LanguageHelper.applySavedLanguage(this);
        this.binding = ActivityProductListBinding.inflate(getLayoutInflater());
        setContentView(this.binding.getRoot());

        this.recyclerProductListAdpter = new RecyclerProductListAdpter(this, MyApp.getInstance().getProductList());
        this.binding.recyclerProductList.setAdapter(recyclerProductListAdpter);
        this.binding.recyclerProductList.setLayoutManager(new LinearLayoutManager(this));

        binding.buttonBack.setOnClickListener(v -> finish());
        binding.buttonReload.setOnClickListener(new ButtonReloadOnClick(getHandlerMain()));

        if (MyApp.getInstance().getProductList().isEmpty()) {
            ProductsDao productsDao = new ProductsDao(this);
            for (ProductStructure product: productsDao.getAll()) {
                MyApp.getInstance().getProductList().add(product);
            }
            productsDao.close();
        }

        if (!MyApp.getInstance().getProductList().isEmpty()) {
            this.recyclerProductListAdpter.notifyItemInserted(MyApp.getInstance().getProductList().size() - 1);
        }
    }

    private class ButtonReloadOnClick extends HandlerMainSchedulerOnClick {
        public ButtonReloadOnClick(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void close(HandlerMain handlerMain) {
            handlerMain.start(getClass().getSimpleName(), getCtx().getString(R.string.successfully_loaded) + MyApp.getInstance().getProductList().size() + "/" + MyApp.getInstance().getProductList().size() + getCtx().getString(R.string.product_information));//成功載入,筆商品圖片
            handlerMain.start(getClass().getSimpleName(), Option.RESET_DATA.getOption(), "reset data");
        }

        @Override
        protected void execute(HandlerMain handlerMain) {
            handlerMain.start(getClass().getSimpleName(),getCtx().getString(R.string.start_downloading_product_images));//開始下載商品圖片

            for (int i = 0; i < MyApp.getInstance().getProductList().size(); i++) {
                ProductStructure product = MyApp.getInstance().getProductList().get(i);
                if (product.getProductImg().isEmpty()) continue;

                try {
                    new ImageGlide(handlerMain.getContext()).preload(product.getProductImg());
                } catch (LogsEmptyException e) {
                    getLogs().warning(e);
                }

                if (i % 10 == 0) {
                    handlerMain.start(getClass().getSimpleName(), getCtx().getString(R.string.successfully_loaded) + i + "/" + MyApp.getInstance().getProductList().size() + getCtx().getString(R.string.product_images));//成功載入,筆商品圖片
                }
            }
        }

        @Override
        public void onClick(View v) {
            start();
        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }
}